package com.hyperfix.washer.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

import static com.hyperfix.models.constant.system.SystemConstant.POLLER_THREAD_COUNT;

/**
 * @author JiangChunYu
 * 2024/4/30
 */
@Configuration
public class AsyncPoolConfig {
    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(POLLER_THREAD_COUNT * 2 + 1);
        executor.setMaxPoolSize(POLLER_THREAD_COUNT * 4);
        executor.setQueueCapacity(POLLER_THREAD_COUNT * 4);
        executor.setKeepAliveSeconds(10);
        executor.setThreadNamePrefix("washer_");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
